﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using System.Xml.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace YEngineLib.Animation
{
    public sealed class YAnimationLoader
    {
        public Dictionary<string, YAnimationData> animDict = new Dictionary<string, YAnimationData>();
        public List<string> animNames = new List<string>();

        private static readonly YAnimationLoader instance = new YAnimationLoader();

        public static YAnimationLoader Instance 
        {
            get
            {
                return instance;
            }
        }
        
        public void LoadAnimationFromXml(string xmlFilePath)
        {
            Stream stream = TitleContainer.OpenStream(xmlFilePath);//@"Content/" + filepath);
            XDocument document = XDocument.Load(stream);

            XElement docRoot = document.Root;

            string animName = (string)docRoot.Attribute("name");

            animName = animName.ToLower();

            // Just don't load animation again
            if (animDict.ContainsKey(animName))
                return;

            bool isLooping = (bool)docRoot.Attribute("loop");
            bool isOscillating = (bool)docRoot.Attribute("oscillate");

            string imageFilePath = docRoot.Element("image").Value;

            Texture2D image = YEngine.Game.Content.Load<Texture2D>(imageFilePath);

            YAnimationData animData = new YAnimationData(image, isLooping, isOscillating);

            XElement framesElm = docRoot.Element("frames");
            IEnumerable<XElement> frames = framesElm.Elements("frame");

            foreach (XElement frame in frames)
            {
                float duration = (float)frame.Attribute("duration");

                XElement sourceRectElm = frame.Element("source_rect");
                
                int x = (int)sourceRectElm.Attribute("x");
                int y = (int)sourceRectElm.Attribute("y");
                int w = (int)sourceRectElm.Attribute("width");
                int h = (int)sourceRectElm.Attribute("height");

                Rectangle sourceRect = new Rectangle(x, y, w, h);

                XElement anchorPointElm = frame.Element("anchor_point");
                int ax = (int)anchorPointElm.Attribute("x");
                int ay = (int)anchorPointElm.Attribute("y");

                Vector2 anchorPoint = new Vector2(ax, ay);

                animData.AddFrame(sourceRect, anchorPoint, duration);
            }

            animData.name = animName;

            animDict.Add(animName, animData);
        }
        
        public YAnimationData GetAnimationData(string animName)
        {
            animName = animName.ToLower();

            if (animDict.ContainsKey(animName))
                return animDict[animName];
            else
                return null;
        }
    }
}
